<template>
  <div id="dailylist">
    <div class="header">
      <div class="calendar">
        <div class="vertical1"></div>
        <div class="vertical2"></div>
        <div class="transverse"></div>
        <div class="date">{{date}}</div>
      </div>
      <div class="title">
        <div class="bold">每日歌曲推荐</div>
        <div class="small">根据你的口味生成，每天6：00更新</div>
      </div>
      <div class="button">
        <!-- <button @click="addlist">添加到列表</button> -->
      </div>
    </div>
    <div class="content">
      <ul>
        <li id="biaoti">
          <span id="index"></span>
          <span id="name">音乐标题</span>
          <span id="author">歌手</span>
          <span id="album">专辑</span>
          <span id="td">时长</span>
        </li>
        <li v-for="(i, index) in tableData" :key="index" @dblclick="clicked(i.id)">
          <span id="index">{{ index + 1 }}</span>
          <span id="name">{{ i.name }}</span>
          <span id="author">{{ i.author }}</span>
          <span id="album">{{ i.album }}</span>
          <span id="td">{{ i.time }}</span>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import axios from "axios";
import formatSeconds from "../js/time";
export default {
  name: "DailyList",
  data() {
    return {
      tableData: [],
      currentRow: null,
      date:""
    };
  },
  computed:{
    ...mapState("songAbout", ["index", "singinglist"]),
  },

  methods: {
    setCurrent(row) {
      this.$refs.singleTable.setCurrentRow(row);
    },
    handleCurrentChange(val) {
      this.currentRow = val;
    },
    getindex(index) {
      let i = index + 1;
      i < 10 ? (i = "0" + i) : (i = i);
      return i;
    },
    demo() {
      alert(123456);
    },
    clicked(id){
      //改变播放状态 playing改为true
      this.$store.commit("songAbout/CHANGE", true);
      //遍历当前播放列表数组，查看点击的歌曲是否存在当前歌单
      let result = this.singinglist.findIndex((item) => {
        return item.id === id;
      });
      if (result == -1) {
        //当前播放列表不包含该歌曲,则添加到当前列表
        axios
          .get("http://localhost:8013/api/song/detail?ids=" + id)
          .then((res) => {
            this.singinglist.splice(this.index + 1, 0, res.data.songs[0]);
            this.$store.dispatch("songAbout/playsongid", this.index + 1);
          });
      } else {
        //当前播放列表包含该歌曲
        this.$store.dispatch("songAbout/playsongid", result);
      }
    },
    // addlist() {},
  },
  mounted() {
    this.date = new Date().getDate();
    console.log(this.date);
    axios.get("http://localhost:8013/api/recommend/songs").then((res) => {
      res.data.data.dailySongs.forEach((item) => {
        const obj = {};
        obj.id = item.id;
        obj.name = item.name;
        obj.author = item.ar[0].name;
        obj.album = item.al.name;
        let t = formatSeconds(item.dt);
        obj.time = t;
        this.tableData.push(obj);
      });
    });
  },
};
</script>

<style scoped>
#dailylist {
  line-height: 20px;
  text-align: left;
}
.header {
  height: 170px;
  border-bottom: 1px solid #eee;
}
.calendar {
  width: 80px;
  height: 70px;
  margin: 25px 20px 0 40px;
  float: left;
  border: 4px solid #b3c0d1;
  border-radius: 20px;
  position: relative;
}
.calendar .vertical1,
.calendar .vertical2 {
  width: 4px;
  height: 20px;
  position: absolute;
  background: #b3c0d1;
  top: -10px;
}
.calendar .vertical1 {
  left: 20px;
}
.calendar .vertical2 {
  left: 56px;
}
.calendar .transverse {
  width: 50px;
  height: 4px;
  background: #b3c0d1;
  position: absolute;
  top: 18px;
  left: 15px;
}
.calendar .date {
  width: 80px;
  height: 70px;
  text-align: center;
  line-height: 90px;
  font-size: 30px;
  font-weight: 600;
  color: #b3c0d1;
}
.header .bold {
  height: 60px;
  line-height: 90px;
  font-size: 20px;
  font-weight: 600;
}
.header .small {
  font-size: 13px;
  color: #888;
  height: 50px;
}
.header .button {
  height: 40px;
}
.header .button button {
  border: none;
  outline: none;
  border-radius: 20px;
  width: 120px;
  height: 30px;
  background: #b3c0d1;
  color: #fff;
  margin: 10px 0px 0px 25px;
}
.content ul li {
  height: 40px;
  line-height: 40px;
  font-size: 16px;
  overflow: hidden;
}
.content ul li:nth-of-type(even) {
  background: #fcfcfc;
}
.content ul li:hover {
  background: #fafafa;
}
.content #biaoti {
  font-weight: 600;
}
.content ul li span {
  display: inline-block;
  font-size: 14px;
  color: #666;
  overflow: hidden;
}
.content ul li #index,
.content ul li #name,
.content ul li #author,
.content ul li #album,
.content ul li #td {
  overflow: hidden;
  height: 40px;
}
.content ul li #index {
  width: 5%;
  color: #aaa;
  text-align: center;
}
.content ul li #name {
  width: 42%;
}
.content ul li #author {
  width: 18%;
}
.content ul li #album {
  width: 25%;
}
.content ul li #td {
  width: 10%;
}
</style>